/**
 * @file 登录相关接口
 * @author its-wild(https://gitee.com/its-wild)
 * @date 2022/01/31
 */
import { apis } from '@/assets/ts/config'
import { post } from '@/plugins/axios'

/**
 * 接口前缀
 */
const { sso } = apis

interface IVerifyCode {
  type: string
}

export interface ICode {
  key: string
  value?: string
}

/**
 * 获取图形验证码/手机验证码
 * @param {IVerifyCode} data 请求参数
 * @returns Promise
 */
export const loadVerifyCode = (data: IVerifyCode): Promise<ICode> => {
  return post(`${sso}verify-code/get`, data)
}

export interface ILogin {
  mobile: string
  password: string
}

interface IUserRelation {
  createTime: string
  enable: number
  extId: string
  id: string
  type: number
  updateTime: string
  userId: string
}

export interface IUser {
  account: string
  avatarStoreId: string
  email: string
  gender: string
  loginTime: string
  mobile: string
  name: string
  realName: string
  regTime: string
  sid: string
  token: string
  uid: string
  userRelations: Array<IUserRelation>
}

/**
 * 登录
 * @param {ILogin} data 请求参数
 * @returns Promise
 */
export const login = (data: ILogin): Promise<IUser> => {
  return post(`${sso}login/mobile-vc/v2`, data)
}

interface ISms {
  mobile: string
  vcCode: string
}

/**
 * 手机验证码登录
 * @param {ISms} data 请求参数
 * @returns Promise
 */
export const sms = (data: ISms): Promise<IUser> => {
  return post(`${sso}login/sms/v2`, data)
}
